
<template>
  <div class="container">
    <div class="login_box">
      <!-- 登录表单 -->
      <div class="login_form">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              type="text"
              v-model="ruleForm.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
              @keydown.enter.native="submitForm('ruleForm')"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { userLogin } from "@/api/user";

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: validatePass, trigger: "blur" }],
        password: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 处理登录成功的逻辑
          console.log(this.ruleForm);
          // 拿到用户名密码信息，然后发送请求到后端进行验证
          userLogin(this.ruleForm).then((res) => {
            console.log(res);
            if (res.code === 200) {
              // 登录成功
              this.$message({
                message: "登录成功",
                type: "success",
                duration: 2000,
              });
              // 跳转到首页
              this.$router.push("/home");
              // 将token保存到本地
              localStorage.setItem("token", res.data.token);
              // 将用户名保存到本地
              localStorage.setItem("username", res.data.username);
            }else if(res.code === 500){
              // 登录失败
              this.$message({
                message: "登录失败,请检查用户名和密码",
                type: "error",
                duration: 2000,
              })
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  
};
</script>

<style lang="less" scoped>
// .el-form-item__label {
//   color: #fff;
// }
.el-button {
  width: 100%;
}

.container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("@/assets/image/bg.jpg") no-repeat;
  background-size: 100% 100%;

  .login_box {
    width: 35%;
    height: 40%;
    background-color: rgba(0, 0, 0, 0.4);
    border-radius: 10px;
    display: flex;
    justify-content: center;
    align-items: center;

    .login_form {
      width: 80%;
      height: 80%;
      padding: 50px 0;
    }
  }
}
</style>